package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ProVideoAdapter;
import flc.ast.databinding.ActivityProVideoBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;
import stark.common.basic.utils.UriUtil;

public class ProVideoActivity extends BaseAc<ActivityProVideoBinding> {

    public static List<SelectMediaEntity> listPlay = new ArrayList<>();//播放列表
    private ProVideoAdapter proVideoAdapter;
    private String videoPath = "";
    private long videoLength;
    private int selPlayPos = 0;
    private CastScreenManager mCastScreenManager;

    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.pbPlay.setProgress(mDataBinding.videoView.getCurrentPosition()/1000);
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_pro_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.rvProVideoList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        proVideoAdapter = new ProVideoAdapter();
        for (SelectMediaEntity bean : listPlay) {
            bean.setChecked(false);
        }
        listPlay.get(0).setChecked(true);
        proVideoAdapter.setList(listPlay);
        mDataBinding.rvProVideoList.setAdapter(proVideoAdapter);
        proVideoAdapter.setOnItemClickListener(this);
        mCastScreenManager = CastScreenManager.getInstance();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPlay://暂停/播放
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlay.setImageResource(R.drawable.bofang1);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivPlay.setImageResource(R.drawable.zanting1);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        proVideoAdapter.getItem(selPlayPos).setChecked(false);
        proVideoAdapter.notifyItemChanged(selPlayPos);
        selPlayPos = position;
        proVideoAdapter.getItem(position).setChecked(true);
        proVideoAdapter.notifyItemChanged(position);
        proVideo(proVideoAdapter.getItem(selPlayPos).getPath());
        setPlayer();
        saveRecord(listPlay.get(position));
    }

    @Override
    protected void initData() {
        if (listPlay == null || listPlay.size() == 0) return;
        //设置播放器
        setPlayer();
        proVideo(listPlay.get(0).getPath());
        saveRecord(listPlay.get(0));
    }

    //视频投屏
    private void proVideo(String url) {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_VIDEO);
        Uri uri = Uri.parse(url);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(url);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

    //保存记录
    private void saveRecord(SelectMediaEntity bean){
        List<SelectMediaEntity> listSave = new ArrayList<>();
        listSave.add(bean);
        List<SelectMediaEntity> listSp = AppSpUtils.getVideoList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveVideoList(listSp);
        } else {
            AppSpUtils.saveVideoList(listSave);
        }
    }

    private void setPlayer() {
        mHandler = new Handler();
        videoPath=listPlay.get(selPlayPos).getPath();
        videoLength = MediaUtil.getDuration(videoPath);
        mDataBinding.pbPlay.setMax((int) (videoLength/1000));
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.ivPlay.setImageResource(R.drawable.bofang1);
                mDataBinding.pbPlay.setProgress(0);
                mDataBinding.videoView.seekTo(1);
                stopTime();
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlay.setImageResource(R.drawable.zanting1);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlay.setImageResource(R.drawable.bofang1);
        mDataBinding.videoView.pause();
        stopTime();
    }

}